$(() => {
    let area_id = GetQueryString('area_id');
    $('.area_id').attr('data-area_id', area_id);
    let keywords = GetQueryString('keys');
    $('.keys').attr('data-keys', keywords);
    let token = GetQueryString('token');
    let page = 1;
    let pageSize = 5;
    companyLists(page, area_id, keywords);

    function companyLists(page, area_id, keywords) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Plat|index_search_more_company", 'area_id': area_id, 'p': page, 'page_size': pageSize, 'keywords': keywords },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    $('.hetitle').html(res.title);
                    data.list.forEach(item => {
                        html += `
                        <div class="comList">`
                        if (item.company_level >= 1) {
                            html += `<a href="shop_details_company.html?company_id=${item.id}&token=${token}" class="comHead">`
                        } else {
                            html += `<a href="shop_details.html?company_id=${item.id}&token=${token}" class="comHead">`
                        }
                        html += `<div class="comHeadLeft">
                                    <img class="headImg" src="${item.company_logo == '' ? '../img/user.jpg' : item.company_logo}" alt="${item.company_name}">
                                    <div class="headlineTopChild">
                                        <p>${item.company_name}</p>`
                        if (item.level_logo != '') {
                            html += `<img src="${item.level_logo}" alt="${item.level_name}" class="userLevel"/>`
                        }
                        html += `</div>
                                </div>
                                <p class="comHeadRight">进店</p>
                            </a>
                            <div class="comImgs">`
                        item.goods_list.forEach(g => {
                            html += `<a href="comp_shopDetail.html?id=${g.id}"><img class="comImg" src="${g.goods_img == '' ? '../img/user.jpg' : g.goods_img}" alt="${g.goods_name}"></a>`
                        })

                        html += `</div>
                            <p class="comDesc">
                                主营：${item.main_business}
                            </p>
                            <p class="comAddr">
                                地址：${item.area_name}
                                <span>浏览量${item.read_count}</span>
                            </p>
                        </div>
                    `;
                    })
                    if (data.list.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.comLists').append(html);
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    $('.search').on('click', function() {
        location.href = "./search.html?token=" + token;
    })

    // 下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                let area_id = $('.area_id').attr('data-area_id');
                let keys = $('.keys').attr('data-keys');
                if (area_id || keys) {
                    companyLists(page, area_id, keys);
                } else {
                    companyLists(page);
                }
            }
        }
    })
})